/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Connection;

//database imports
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

//XML imports
import java.io.File;
import java.io.IOException;
import java.sql.*;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 *
 * @author BETA911
 */
public class DataHandler {
    private static String URL;
    private static String DB_NAME;
    private static String USERNAME;
    private static String PASSWORD;
    public static Connection con = null;
    public static Statement statement = null;
    public static ResultSet rs = null;
    public static ResultSetMetaData md = null;
    public static boolean xmlFileExists = false;
    
    public DataHandler() throws IOException {
        //read the database information from seperate XML file
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            File dbXml = new File("DatabaseInformation.xml");
            if (dbXml.exists()){
                xmlFileExists = true;
                Document doc = docBuilder.parse (dbXml);
                doc.getDocumentElement().normalize();
                URL = doc.getElementById("url").getNodeValue();
                DB_NAME = doc.getElementById("dbname").getNodeValue();
                USERNAME = doc.getElementById("username").getNodeValue();
                PASSWORD = doc.getElementById("password").getNodeValue();
            } else {
                xmlFileExists = false;
                //Manually set up the info temporarily if the XML is not found!!
                URL = "jdbc:mysql://localhost/";
                DB_NAME = "netbooks_groep18";
                USERNAME = "root";
                PASSWORD = "";
            }
        } catch (Exception ex) {
        }
    }//end the contructor

    //test (debug) methodes...
    public static boolean getXmlFileExists() {
        return xmlFileExists;
    }
    public static String getConnectionInfo(){
        return "<p><b>URL:</b> " + URL + "</br>"
               + "<b>Database:</b> " + DB_NAME + "</br>"
               + "<b>Username:</b> " + USERNAME + "</br>"
               + "<b>Password:</b> " + PASSWORD + "</br>"
               + "</p>";
    }

    public DataHandler(String url, String dbname, String username, String password) {
        this.URL = url;
        this.DB_NAME = dbname;
        this.USERNAME = username;
        this.PASSWORD = password;
    }

    public void startConnectie() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection(URL + DB_NAME, USERNAME, PASSWORD);
        } catch (Exception ex){
        } finally {
            stopConnectie();
        }        
    }

    public void stopConnectie() {
        try {
            con.close();
        } catch (Exception ex) {
            //foutmelding als de database niet kan sluiten
        }
    }

    public ResultSet voerQueryUit(String query) throws SQLException {
        statement = con.createStatement();
        rs = statement.executeQuery(query);
        return rs;
    }

    public void voerUpdateUit(String uQuery) throws SQLException {
        statement = con.createStatement();
        statement.executeUpdate(uQuery);
    }
}
